#include "daily_widget.h"
#include <QIcon>
#include <QPixmap>
#include <QPainter>
#include <QStyleOption>
#include "stydatabase.h"

daily_widget::daily_widget(QWidget *parent) : QWidget(parent)
{
    newDynamic = new new_dynamic;
    /* 1.新建滑动页面 */
    scroll = new QScrollArea();
    scroll->setFrameShape(QFrame::StyledPanel);
    scroll->setWidgetResizable(true);

    scrollAreaWidgetContents = new QWidget();
    scrollAreaWidgetContents->setObjectName("scrollAreaWidgetContentsDaily");
    scroll->setWidget(scrollAreaWidgetContents);

    /* 2.添加一个新建按钮 */
    plusBtn = new QPushButton;
    plusBtn->setObjectName("plusBtn");
    topLay = new QHBoxLayout;
    topLay->addStretch();
    topLay->addWidget(plusBtn);
    topLay->addSpacing(15);

    /* 3.设置布局 */
    mainLay = new QVBoxLayout;
    mainLay->setMargin(0);
    mainLay->setSpacing(0);
    mainLay->addWidget(scroll);
    this->setLayout(mainLay);

    layout = new QVBoxLayout(scrollAreaWidgetContents);
    layout->setSpacing(0);
    layout->setMargin(5);
    scrollAreaWidgetContents->setLayout(layout);

    plusBtn->setText("+");
    connect(this->plusBtn,SIGNAL(clicked(bool)),this,SLOT(onBtnPlusClicked(bool)));
    connect(this->newDynamic,SIGNAL(onNewDynamic(QString,QString)),this,SLOT(onNewDynamicEmit(QString,QString)));

    getAllDailySummary(allDailySummary);
    initAllDailySummary();
    newDynamic->hide();
    //newDynamic->show();
}

void daily_widget::paintEvent(QPaintEvent *event)
{
    this->scroll->resize(this->parentWidget()->width(),this->parentWidget()->height());
    this->scrollAreaWidgetContents->resize(this->parentWidget()->width(),
                        this->parentWidget()->height()*allDailySummary.size());
    QStyleOption opt;
    opt.init(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}

void daily_widget::initAllDailySummary()
{
    qDebug()<<"allDailySummary.size() = "<<allDailySummary.size();
    layout->addLayout(topLay);
    if(allDailySummary.size()==0){
        layout->addStretch();
    }
    for(int i=0;i<allDailySummary.size();i++){
        one_dynamic *one = new one_dynamic(allDailySummary[i]);
        layout->addWidget(one);
    }
}

void daily_widget::onBtnPlusClicked(bool on)
{
    qDebug()<<"发布新动态";

    newDynamic->show();
}

void daily_widget::onNewDynamicEmit(QString content,QString file_name)
{
    struct DailySummary one;
    one.DContent=content;
    one.DDate=QDate::currentDate();
    one.DTime=QTime::currentTime();
    one.DName=g_data[0].friends.UNickname;
    one.DPicture.load(file_name);
    one.DPraise="";
    one_dynamic *oneDy = new one_dynamic(one);
    layout->addWidget(oneDy);
}

